package com.dabay.web.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dabay.web.wsdlserverwork.WsServiceLocator;
import com.dabay.web.wsdlserverwork.WsServiceSoapBindingStub;
import com.dabay.web.Model.Bybt;
import com.dabay.web.Model.CommitResult;
import com.dabay.web.Model.Dept;
import com.dabay.web.Model.Dmsj;
import com.dabay.web.Model.GangWei;
import com.dabay.web.Model.Sbbt;
import com.dabay.web.Model.Wxts;
import com.dabay.web.util.Helper;
import com.dabay.web.util.ResultSupportWork;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class WebService {
	private static Logger logger = LoggerFactory.getLogger(WebService.class);
	private XmlParserService xmlParser = new XmlParserService();
	private Helper helper = new Helper();
	private String fenye = "※";

	//4.12创业人员社保补贴－数据报送接口
	public  ResultSupportWork commit_cyzsbbt(Sbbt sbbt){
		String methodName = "CY_SBBT_SB";
		String paramStr = "<SJ>";
				paramStr +=  "<BBC017>"+sbbt.getBbc017()+"</BBC017>";
				paramStr +=  "<AAE004>"+sbbt.getAae004()+"</AAE004>";
				paramStr +=  "<AAB003>"+sbbt.getAab003()+"</AAB003>";
				paramStr +=  "<R00046>"+sbbt.getR00046()+"</R00046>";
				paramStr +=  "<AAE006>"+sbbt.getAae006()+"</AAE006>";
				paramStr +=  "<R00048>"+sbbt.getR00048()+"</R00048>";
				paramStr +=  "<AAC002>"+sbbt.getAac002()+"</AAC002>";
				paramStr +=  "<AAC003>"+sbbt.getAac003()+"</AAC003>";
				paramStr +=  "<R0004B>"+sbbt.getR0004b()+"</R0004B>";
				paramStr +=  "<R0004H>"+sbbt.getR0004h()+"</R0004H>";
				paramStr +=  "<R0004J>"+sbbt.getR0004j()+"</R0004J>";
				paramStr +=  "<R0004D>"+sbbt.getR0004d()+"</R0004D>";
				paramStr +=  "<R0005P>"+sbbt.getR0005p()+"</R0005P>";
				paramStr +=  "<R0005Q>"+sbbt.getR0005q()+"</R0005Q>";
				paramStr +=  "<R0005R>"+sbbt.getR0005r()+"</R0005R>";
			paramStr += "</SJ>";
		ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
		 List<CommitResult> dataList = new ArrayList<CommitResult>();
		 JSONArray dataArray = (JSONArray)support.getModel();
		 if(dataArray!=null){
			 for (int i = 0; i < dataArray.size(); i++) {  
		    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
		    	 logger.info("调用返回数据"+resultStr);
				 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase()); 
				 CommitResult tempobj = (CommitResult)JSONObject.toBean(object,CommitResult.class);
		         if(tempobj != null){
		        	 dataList.add(tempobj);
		         }  
		      } 
		 }
	  support.setModel(dataList);
	  logger.info("调用返回码："+support.getResultCode());
	  logger.info("返回条数："+support.getTotalCount());
	  logger.info("调用返回消息："+support.getMessage());
	  return support;
	}	
	//4.15高校生就业补贴－数据报送接口
	public  ResultSupportWork commit_gxssbbt(Bybt bybt){
		String methodName = "GX_JYBT_SB";
		String paramStr = "<SJ>";
			paramStr +=  "<BBC017>"+bybt.getBbc017()+"</BBC017>";
			paramStr +=  "<AAE017>"+bybt.getAae017()+"</AAE017>";
			paramStr +=  "<AAE004>"+bybt.getAae004()+"</AAE004>";
			paramStr +=  "<AAB003>"+bybt.getAab003()+"</AAB003>";
			paramStr +=  "<AAB007>"+bybt.getAab007()+"</AAB007>";
			paramStr +=  "<AAC002>"+bybt.getAac002()+"</AAC002>";
			paramStr +=  "<AAC003>"+bybt.getAac003()+"</AAC003>";
			paramStr +=  "<BBC018>"+bybt.getBbc018()+"</BBC018>";
			paramStr +=  "<BBC023>"+bybt.getBbc023()+"</BBC023>";
			paramStr +=  "<BBC019>"+bybt.getBbc019()+"</BBC019>";
			paramStr +=  "<BBC022>"+bybt.getBbc022()+"</BBC022>";
			paramStr +=  "<R0005R>"+bybt.getR0005r()+"</R0005R>";
			paramStr += "</SJ>";
		ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
		 List<CommitResult> dataList = new ArrayList<CommitResult>();
		 JSONArray dataArray = (JSONArray)support.getModel();
		 if(dataArray!=null){
			 for (int i = 0; i < dataArray.size(); i++) {  
		    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
		    	 logger.info("调用返回数据"+resultStr);
				 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
				 CommitResult tempobj = (CommitResult)JSONObject.toBean(object,CommitResult.class);
		         if(tempobj != null){
		        	 dataList.add(tempobj);
		         }  
		      } 
		 }
	  support.setModel(dataList);
	  logger.info("调用返回码："+support.getResultCode());
	  logger.info("返回条数："+support.getTotalCount());
	  logger.info("调用返回消息："+support.getMessage());
	  return support;
	}	
	
//4.13 创业者社保补贴－数据查询接口
public  ResultSupportWork send_cyzsbbt(String lx,String hm){
	String methodName = "CY_SBBT_CX";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<Sbbt> dataList = new ArrayList<Sbbt>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
	         Sbbt tempobj = (Sbbt)JSONObject.toBean(object,Sbbt.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
  support.setModel(dataList);
  logger.info("调用返回码："+support.getResultCode());
  logger.info("返回条数："+support.getTotalCount());
  logger.info("调用返回消息："+support.getMessage());
  return support;
}

//4.16 高校生就业补贴－数据查询接口
public  ResultSupportWork send_gxsbybt(String lx,String hm){
	String methodName = "GX_JYBT_CX";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<Bybt> dataList = new ArrayList<Bybt>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
			 Bybt tempobj = (Bybt)JSONObject.toBean(object,Bybt.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
	  support.setModel(dataList);
	  logger.info("调用返回码："+support.getResultCode());
	  logger.info("返回条数："+support.getTotalCount());
	  logger.info("调用返回消息："+support.getMessage());
	  return support;
}

//4.14 创业者社保补贴－温馨提示查询接口
public  ResultSupportWork send_cyzsbbt_tishi(String lx,String hm){
	String methodName = "CY_SBBT_WXTS_CX";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<Wxts> dataList = new ArrayList<Wxts>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
			 Wxts tempobj = (Wxts)JSONObject.toBean(object,Wxts.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
	support.setModel(dataList);
	logger.info("调用返回码："+support.getResultCode());
	logger.info("返回条数："+support.getTotalCount());
	logger.info("调用返回消息："+support.getMessage());
	return support;
}
//4.17  高校生就业补贴－温馨提示查询接口
public  ResultSupportWork send_gxsjybt_tishi(String lx,String hm){
	String methodName = "GX_JYBT_WXTS_CX";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<Wxts> dataList = new ArrayList<Wxts>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
			 Wxts tempobj = (Wxts)JSONObject.toBean(object,Wxts.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
	support.setModel(dataList);
	logger.info("调用返回码："+support.getResultCode());
	logger.info("返回条数："+support.getTotalCount());
	logger.info("调用返回消息："+support.getMessage());
	return support;
}

//4.5机构数据测试（高校毕业生就业补贴）申报地点
public  ResultSupportWork queryDept(String lx,String hm){
	hm = hm.toUpperCase();
	String methodName = "DM_JG_CX";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<Dept> dataList = new ArrayList<Dept>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase()); 
			 Dept tempobj = (Dept)JSONObject.toBean(object,Dept.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
	support.setModel(dataList);
	logger.info("调用返回码："+support.getResultCode());
	logger.info("返回条数："+support.getTotalCount());
	logger.info("调用返回消息："+support.getMessage());
	return support;
}
public  ResultSupportWork sendWebServiceReq(String reqMethod,String reqParamStr){
	logger.info("请求方法"+reqMethod);
	logger.info("请求参数"+reqParamStr);
	ResultSupportWork support = new ResultSupportWork();
	try {
		WsServiceLocator service = new WsServiceLocator();
		java.net.URL url = new java.net.URL(Constant.WS_URL);
		WsServiceSoapBindingStub stub = new WsServiceSoapBindingStub(url, service);
		String paramStr = getReqHead(reqMethod);
		paramStr += reqParamStr;
		paramStr += getReqEnd();
		String xmlStr = stub.tzc_sys_api(Constant.WS_ID, paramStr);
		String xmlStrToJsonStr = xmlParser.xml2JSON(xmlStr);
		Map resultMap= xmlParser.headResponseDeal(xmlStrToJsonStr);
		//消息头
		support.setResultCode(helper.parseObjToStr(resultMap.get("FHM")));
		support.setMessage(helper.parseObjToStr(resultMap.get("FHSM")));
		//消息数据
		JSONArray  jsonArray = xmlParser.getDataArray(xmlStrToJsonStr);
		support.setModel(jsonArray);
		if(jsonArray!=null){
			support.setTotalCount(jsonArray.size()+"");
		}else{
			support.setTotalCount("");
		}
		return support;
	} catch (Exception e) {
		support.setResultCode("ERROR");
		support.setMessage("解析失败！"+e.fillInStackTrace());
		return support;
	}
}
//4.20  稳定岗位补贴资格审核查询
public  ResultSupportWork send_wgbt_zgsh(String lx,String hm){
	String methodName = "SY_WGBT_ZGSH";
	String paramStr = "<LX>"+lx+"</LX><HM>"+hm+"</HM><FY>"+fenye+"</FY>";
	ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
	 List<CommitResult> dataList = new ArrayList<CommitResult>();
	 JSONArray dataArray = (JSONArray)support.getModel();
	 if(dataArray!=null){
		 for (int i = 0; i < dataArray.size(); i++) {  
	    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
	    	 logger.info("调用返回数据"+resultStr);
			 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
			 CommitResult tempobj = (CommitResult)JSONObject.toBean(object,CommitResult.class);
	         if(tempobj != null){
	        	 dataList.add(tempobj);
	         }  
	      } 
	 }
	support.setModel(dataList);
	logger.info("调用返回码："+support.getResultCode());
	logger.info("返回条数："+support.getTotalCount());
	logger.info("调用返回消息："+support.getMessage());
	return support;
}

//4.9 业委托招聘热门岗位数据查询接口
	public  ResultSupportWork send_hotwork_query(String paramStr){
		String methodName = "ZYJS_WTZP_CX";
		ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
		 List<GangWei> dataList = new ArrayList<GangWei>();
		 JSONArray dataArray = (JSONArray)support.getModel();
		 if(dataArray!=null){
			 for (int i = 0; i < dataArray.size(); i++) {  
		    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
		    	 logger.info("调用返回数据"+resultStr);
				 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
				 GangWei tempobj = (GangWei)JSONObject.toBean(object,GangWei.class);
		         if(tempobj != null){
		        	 tempobj.setDetailstr(resultStr.toLowerCase());
		        	 //计算总招聘人数
		        	 String total_renshuStr = helper.parseObjToInt(tempobj.getAcb21f())
		        			 +helper.parseObjToInt(tempobj.getAcb21d())
		        			 +helper.parseObjToInt(tempobj.getAcb21e())+"";
		        	 tempobj.setTotal_renshu(total_renshuStr);
		        	 dataList.add(tempobj);
		         }  
		      } 
		 }
		support.setModel(dataList);
		logger.info("调用返回码："+support.getResultCode());
		logger.info("返回条数："+support.getTotalCount());
		logger.info("调用返回消息："+support.getMessage());
		return support;
	}
	
	//4.4代码数据查询
		public  ResultSupportWork dm_query(String dml,String dq){
			String methodName = "DM_CX";
			String paramStr = "<DML>"+dml+"</DML><DQ>"+dq+"</DQ><FY>1※20</FY>";
			ResultSupportWork support = sendWebServiceReq(methodName,paramStr);
			 List<Dmsj> dataList = new ArrayList<Dmsj>();
			 JSONArray dataArray = (JSONArray)support.getModel();
			 if(dataArray!=null){
				 for (int i = 0; i < dataArray.size(); i++) {  
			    	 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
			    	 logger.info("调用返回数据"+resultStr);
					 JSONObject object = JSONObject.fromObject(resultStr.toLowerCase());
					 Dmsj tempobj = (Dmsj)JSONObject.toBean(object,Dmsj.class);
					 dataList.add(tempobj);
			      } 
			 }
			support.setModel(dataList);
			logger.info("调用返回码："+support.getResultCode());
			logger.info("返回条数："+support.getTotalCount());
			logger.info("调用返回消息："+support.getMessage());
			return support;
		}

private static String getReqHead(String fwmStr){
	StringBuffer sb = new StringBuffer();
	sb.append("<?xml version='1.0' encoding='UTF-8'?><TZM><TZH>");
	sb.append("<FWM>"+fwmStr+"</FWM>");
	sb.append("<BBH>"+Constant.WS_VERSION+"</BBH><SID>*</SID></TZH>");
	sb.append("<TZB>");
	return sb.toString();
}
private static String getReqEnd(){
	String endStr = "</TZB></TZM>";
	return endStr;
}
}
